package com.chris.filter;

import com.chris.utils.JsonProcess;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Base64;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/*")
public class allFilter implements Filter {
    public allFilter() {
    }

    public void init(FilterConfig filterConfig) throws ServletException {

    }
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse)servletResponse;
        HttpServletRequest req = (HttpServletRequest)servletRequest;
        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.addHeader("Access-Control-Allow-Headers", "*");
        resp.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        if (!req.getRequestURI().equals("/library/login") && !req.getRequestURI().equals("/library/loginRoot")) {
            String authorization = req.getHeader("authorization");
            if (authorization != null) {
                PrintWriter writer;
                try {
                    byte[] decode = Base64.getDecoder().decode(authorization);
                    if (decode.length == 4) {
                        filterChain.doFilter(servletRequest, servletResponse);
                    } else {
                        writer = resp.getWriter();
                        writer.println(JsonProcess.respError("登录验证错误"));
                    }
                } catch (Exception var9) {
                    var9.printStackTrace();
                    writer = resp.getWriter();
                    writer.println(JsonProcess.respError("登录验证错误"));
                }
            } else {
                PrintWriter writer = resp.getWriter();
                writer.println(JsonProcess.respError("请先登录"));
            }
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }
    public void destroy() {

    }
}
